package com.qrback.controller;

import com.qrback.entity.OperationLog;
import com.qrback.repository.OperationLogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/logs")
public class LogController {

    @Autowired
    private OperationLogRepository operationLogRepository;

    // 获取操作日志
    @GetMapping("/operation")
    public ResponseEntity<?> getOperationLogs(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) String operationType) {
        try {
            // 创建分页和排序对象 (注意：Spring Data JPA的PageRequest是从0开始的页码，这里减1适配前端从1开始的页码)
            Pageable pageable = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Direction.DESC, "operationTime"));
            
            Page<OperationLog> logPage=null;
            
            // 根据筛选条件查询日志
            if (startDate != null && endDate != null && operationType != null) {
                // 同时根据日期范围和操作类型查询
//                logPage = operationLogRepository.findByOperationTimeBetweenAndOperation(
//                        java.sql.Timestamp.valueOf(startDate + " 00:00:00"),
//                        java.sql.Timestamp.valueOf(endDate + " 23:59:59"),
//                        operationType,
//                        pageable);
            } else if (startDate != null && endDate != null) {
                // 根据日期范围查询
//                logPage = operationLogRepository.findByOperationTimeBetween(
//                        java.sql.Timestamp.valueOf(startDate + " 00:00:00"),
//                        java.sql.Timestamp.valueOf(endDate + " 23:59:59"),
//                        pageable);
            } else if (operationType != null) {
                // 根据操作类型查询
                logPage = operationLogRepository.findByOperation(operationType, pageable);
            } else {
                // 查询所有日志
                logPage = operationLogRepository.findAll(pageable);
            }
            
            // 构建返回结果
            Map<String, Object> response = new HashMap<>();
//            response.put("logs", logPage.getContent());
            response.put("total", logPage.getTotalElements());
            response.put("page", page); // 使用前端传入的页码（从1开始）
            response.put("pageSize", pageSize); // 重命名为pageSize以匹配前端
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.status(500).body(e.getMessage());
        }
    }
}